﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backup.Services.Logic;
using BackupObjects.Taging;
using Microsoft.Samples.ServiceHosting.StorageClient;
using NUnit.Framework;

namespace Backup.Services.Tests
{
    [TestFixture]
    public class TagServiceLogicTests
    {
        private static TagRepository _tagRepository;

        private Tag tag1;
        private Tag tag11;
        private Tag tag12;
        private Tag tag2;

        [SetUp]
        public void TagRepositorySetUp()
        {
            _tagRepository = new TagRepository();

            _tagRepository
                            .LoadAll()
                            .ForEach(item => _tagRepository.Delete(item.Id));

            tag1 = new Tag() { Id = Guid.NewGuid(), Name = "Tag1Name" };
            tag11 = new Tag() { Id = Guid.NewGuid(), Name = "Tag11Name", Parent = tag1 };
            tag12 = new Tag() { Id = Guid.NewGuid(), Name = "Tag12Name", Parent = tag1 };
            tag2 = new Tag() { Id = Guid.NewGuid(), Name = "Tag2Name" };

            _tagRepository.Save(tag1);
            _tagRepository.Save(tag11);
            _tagRepository.Save(tag12);
            _tagRepository.Save(tag2);
        }

        [Test]
        public void LoadTest()
        {
            TagServiceLogic tagServiceLogic = new TagServiceLogic();
            var result = tagServiceLogic.Load(tag1.Id);

            Assert.AreEqual(tag1.Id, result.Id, "The tag was not been loaded correctly.");
            Assert.AreEqual(tag1.Name, result.Name, "The tag was not been loaded correctly.");
        }

        [Test]
        public void SaveTest()
        {
            TagServiceLogic tagServiceLogic = new TagServiceLogic();
            Tag originalTag = new Tag()
                          {
                              Name = DateTime.Now.ToString()
                          };

            originalTag = tagServiceLogic.Save(originalTag);
            var savedTag = tagServiceLogic.Load(originalTag.Id);

            Assert.AreNotEqual(Guid.Empty, savedTag.Id);
            Assert.AreEqual(originalTag.Name, savedTag.Name, "The names are not the same");
            Assert.AreEqual(originalTag.Id, savedTag.Id, "The ids are not the same");
        }

        [Test]
        public void DeleteTest()
        {
            TagServiceLogic tagServiceLogic = new TagServiceLogic();
            tagServiceLogic.Delete(tag2);

            var deletedTag = tagServiceLogic.Load(tag2);
            Assert.IsNull(deletedTag);
        }

        [Test]
        public void LoadAllTest()
        {
            TagServiceLogic tagServiceLogic = new TagServiceLogic();
            var tagList = tagServiceLogic.LoadAll();

            Assert.IsNotNull(tagList, "The tag list is null.");
            Assert.AreNotEqual(0, tagList.Count, "The tag list is empty.");
        }

        [Test]
        public void GetChildrens()
        {
            TagServiceLogic tagServiceLogic = new TagServiceLogic();

            var childrens = tagServiceLogic.GetChildrens(tag1);

            Assert.IsNotNull(childrens, "Childrens are null.");
            Assert.AreEqual(2, childrens.Count, "Number of childrens is wrong");
        }

        [Test]
        public void GetParent()
        {
            TagRepository tagRepository = new TagRepository();

            var parentTag = tagRepository.GetParent(tag12);

            Assert.IsNotNull(parentTag, "The parent tag cannot be null.");
            Assert.AreEqual(tag1.Id, parentTag.Id, "The id's are not the same.");
        }
    }
}
